package com.ikun.mallbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikun.mallbackend.entity.*;
import com.ikun.mallbackend.service.IBigTypeService;
import com.ikun.mallbackend.service.IProductService;
import com.ikun.mallbackend.service.IProductSwiperImageService;
import com.ikun.mallbackend.service.ISmallTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品Controller
 * @author java1234_小锋
 * @site www.java1234.com
 * @company 南通小锋网络科技有限公司
 * @create 2022-02-23 22:03
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    private final IProductService productService;
    private final IProductSwiperImageService productSwiperImageService;


    public ProductController(IProductService productService, IProductSwiperImageService productSwiperImageService) {
        this.productService = productService;
        this.productSwiperImageService = productSwiperImageService;
    }

    /**
     * 查询轮播商品
     * @return
     */
    @GetMapping("/findSwiper")
    public Result findSwiper(){
        List<Product> swiperProductList = productService.list(new QueryWrapper<Product>().eq("isSwiper", true).orderByAsc("swiperSort"));
        Map<String,Object> map=new HashMap<>();
        map.put("message",swiperProductList);
        return Result.ok(map);
    }

    /**
     * 返回前8个热卖商品
     * @return
     */
    @GetMapping("/findHot")
    public Result findHot(){
        Page<Product> page = new Page<>(0,8);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>()
                .eq("isHot", 1)
                .orderByAsc("hotDateTime");
        Page<Product> pageProduct = productService.page(page,queryWrapper);
        List<Product> hotProductList = pageProduct.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("message",hotProductList);
        return Result.ok(map);
    }

    /**
     * 根据id获取商品详情
     * @return
     */
    @GetMapping("/detail")
    public Result detail(Integer id){
        Product product = productService.getById(id);
        List<ProductSwiperImage> productSwiperImageList = productSwiperImageService.list(new QueryWrapper<ProductSwiperImage>().eq("productId", product.getId()).orderByAsc("sort"));
        product.setProductSwiperImages(productSwiperImageList);
        Map<String,Object> map=new HashMap<>();
        map.put("message",product);
        return Result.ok(map);
    }

}
